package net.suncaper.demo.service;

import net.suncaper.demo.domain.*;
import net.suncaper.demo.mapper.EvaluateMapper;
import net.suncaper.demo.mapper.ImageMapper;
import net.suncaper.demo.mapper.ProductMapper;
import net.suncaper.demo.service.ProductImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    ImageService imageService;
    @Autowired
    ImageMapper imageMapper;
//    @Autowired
//    CategoryService categoryService;
    @Autowired
    ProductImageService productImageService;
//    @Autowired
//    OrderItemService orderItemService;
//    @Autowired
//    ReviewService reviewService

    @Override
    public List<Product> findProduct() {
        return productMapper.selectByExample(new ProductExample());
    }

    @Override
    public List<Product> findProduct(String name) {
        ProductExample example = new ProductExample();
        if (name != null && !name.equals("")) {
            example.createCriteria().andProductNameLike("%" + name + "%");
        }
        return productMapper.selectByExample(example);
    }

    @Override
    public void saveProduct(Product product) {
        if (product.getProductId()!=null&&!product.getProductId().equals("")){
//            this.productMapper.updateByPrimaryKey(product);
            this.productMapper.insert(product);

        }
       else {
           System.out.println("error");
        }
    }

    @Override
    public void deleteProduct(int id) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andProductIdEqualTo(id);
        productMapper.deleteByExample(productExample);
    }


//    @Override
    @Cacheable("product")
    public Product findProductById(int id) {
        //ProductExample productExample = new ProductExample();
        //productExample.createCriteria().andProductIdEqualTo(id);
        //return  productMapper.selectByExample(productExample).get(0);
        return  productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateProduct(Product product) {
        if (product.getProductId()!=null&&!product.getProductId().equals("")){
            this.productMapper.updateByPrimaryKey(product);
        }
    }

    @Override
    public Product searchProduct(int id) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andProductIdEqualTo(id);
        return  productMapper.selectByExample(productExample).get(0);
    }

    @Override
    public Product get(int id) {
        Product p = productMapper.selectByPrimaryKey(id);
        setFirstProductImage((List<Product>) p);
        return p;
    }

//    @Override
//    public List list(int cid) {
//        ProductExample example = new ProductExample();
//        example.createCriteria().andProductIdEqualTo(cid);//not cid
//        example.setOrderByClause("id desc");
//        List result = productMapper.selectByExample(example);
//        setFirstProductImage(result);
////        setCategory(result);
//        return result;
//    }

    @Override
    public void savePath(Product product, Path file) {
        ProductExample productExample = new ProductExample();
        if (product.getProductId()!=null&&!product.getProductId().equals("")){
            product.setProductPics(file.toString());
            this.productMapper.updateByPrimaryKey(product);
        }
    }

    @Override
    public void setPicnum(Product product) {
        ProductExample productExample = new ProductExample();
        String path = product.getProductPics();
        File file = new File(path);
        int num =file.listFiles().length;
        File[] f=file.listFiles();
        product.setProductPicnum(num);
        this.productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void setMainImage(Product product,String image) {
        ProductExample productExample = new ProductExample();
        product.setMainImage(image);
        this.productMapper.updateByPrimaryKey(product);
    }

//    @Override
//    public List<Evaluate> getEvaluateById(int id) {
//        EvaluateExample evaluateExample=new EvaluateExample();
//        evaluateExample.createCriteria().andUserIdEqualTo(id);
//        return productMapper.selectByExample(evaluateExample);
//    }

//    @Override
//    public void setSaleAndReviewNumber(Product p) {
//        int saleCount = orderItemService.getSaleCount(p.getId());
//        p.setSaleCount(saleCount);
//
//        int reviewCount = reviewService.getCount(p.getId());
//        p.setReviewCount(reviewCount);
//    }

//    @Override
//    public void setSaleAndReviewNumber(List<Product> ps) {
//        for (Product p : ps) {
//            setSaleAndReviewNumber(p);
//        }
//    }



//    @Override
//    public void setFirstProductImage(Product p) {
//        List<ProductImage> pis = productImageService.list(p.getProductId(), ProductImageService.type_single);
//        if (!pis.isEmpty()) {
//            ProductImage pi = pis.get(0);
//            p.setFirstProductImage(pi);
//        }
//    }

    public void setFirstProductImage(List<Product> ps) {
        for (Product p : ps) {
            setFirstProductImage((List<Product>) p);
        }
    }

    @Override
    public  List<Product> findProductByType(String type) {
        ProductExample productExample = new ProductExample();
        if (type != null && !type.equals("")) {
            productExample.createCriteria().andProductTypeLike("%"+type+"%");
        }
        return  productMapper.selectByExample(productExample);
    }

    @Override
    public List<Product> searchProduct(String s) {
        ProductExample productExample = new ProductExample();
        if (s != null && !s.equals("")) {
//            productExample.createCriteria().andProductTypeLike("%"+s+"%");
            productExample.createCriteria().andProductNameLike("%"+s+"%");
        }
        return  productMapper.selectByExample(productExample);
    }

    //查找某店铺所属商品
    @Override
    public List<Product> findProductBystoreId(int storeID) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andStoreIdEqualTo(storeID);
        return productMapper.selectByExample(productExample);
    }

    @Override
    public List<Evaluate> getEvaluatebyId(int id) {
        EvaluateExample evaluateExample = new EvaluateExample();
        evaluateExample.createCriteria().andProductIdEqualTo(id);
        return  evaluateMapper.selectByExample(evaluateExample);
    }

    @Override
    public void addImage(Image image) {
        ImageExample imageExample = new ImageExample();
        imageService.saveImage(image);
    }

    @Override
    public List<Image> getImages(int id) {
        ImageExample imageExample = new ImageExample();
        imageExample.createCriteria().andProductIdEqualTo(id);
        return imageMapper.selectByExample(imageExample);
    }

}
